package com.dreamer.view.wxchat;

import com.dreamer.service.mobile.factory.WxConfigFactory;
import com.dreamer.util.TokenInfo;
import com.wxjssdk.JSAPI;
import com.wxjssdk.dto.SdkResult;
import com.wxjssdk.menu.domain.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by huangfei on 09/07/2017.
 */
@Controller
@RequestMapping("/wechat/menu")
public class MenuController {

    private String url = "ht.zmz365.com/zz/wechat/menu/create";

    @RequestMapping("/create")
    @ResponseBody
    private SdkResult create(@RequestParam(required = false) String menuJson) {
        if (menuJson == null || menuJson.equals("")) {
            WxMenu wxMenu = new WxMenu();

            WxBotLink wxBotLink1 = new WxBotLink();
            wxBotLink1.setType(WxLinkType.view.getStateInfo());
            wxBotLink1.setName("商城入口");
            wxBotLink1.setUrl("http://ht.zmz365.com/zz/dmz/mobile/index.html");
            //商城入口
            WxBotLink wxBotLink0 = new WxBotLink();
            wxBotLink0.setType(WxLinkType.view.getStateInfo());
            wxBotLink0.setName("肤质评估");
            wxBotLink0.setUrl("https://www.wjx.cn/jq/45032991.aspx");


            //查询
            WxTopLink wxTopLink2 = new WxTopLink();
            wxTopLink2.setName("查询入口");
            //0 授权查询
            WxBotLink wxBotLink2_0 = new WxBotLink();
            wxBotLink2_0.setType(WxLinkType.view.getStateInfo());
            wxBotLink2_0.setName("授权查询");
            wxBotLink2_0.setUrl("http://ht.zmz365.com/zz/dmz/agent/search.html?f=1");//验证账号地方
            //防伪查询
            WxBotLink wxBotLink2_1 = new WxBotLink();
            wxBotLink2_1.setType(WxLinkType.view.getStateInfo());
            wxBotLink2_1.setName("防伪码查询");
            wxBotLink2_1.setUrl("http://ht.zmz365.com/zz/dmz/securityCode/search.html?f=1");//验证账号地方

            WxBotLink wxBotLink2_2 = new WxBotLink();
            wxBotLink2_2.setType(WxLinkType.view.getStateInfo());
            wxBotLink2_2.setName("完善信息");
            wxBotLink2_2.setUrl("http://ht.zmz365.com/zz/mobile/register.html");//验证账号地方
            wxTopLink2.setSub_button(new WxBotLink[]{wxBotLink2_0, wxBotLink2_1,wxBotLink2_2});
            //组装
            wxMenu.setButton(new WxLink[]{wxBotLink1,wxBotLink0, wxTopLink2});
            //拼成JSON
            menuJson = JSONObject.fromObject(wxMenu).toString();
        }
        SdkResult sdkResult = JSAPI.createMenu(TokenInfo.getAccessToken(wxConfigFactory.getBaseConfig().getAppid()), menuJson);
        return sdkResult;
    }


    @Autowired
    private WxConfigFactory wxConfigFactory;


}
